using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace LetLord.Models
{ 
    public class ResidentialPropertyRepository : IResidentialPropertyRepository
    {
        LetLordContext context = new LetLordContext();

        public IQueryable<ResidentialProperty> All
        {
            get { return context.ResidentialProperty; }
        }

        public IQueryable<ResidentialProperty> AllIncluding(params Expression<Func<ResidentialProperty, object>>[] includeProperties)
        {
            IQueryable<ResidentialProperty> query = context.ResidentialProperty;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public ResidentialProperty Find(int id)
        {
            return context.ResidentialProperty.Find(id);
        }

        public void InsertOrUpdate(ResidentialProperty residentialproperty)
        {
            if (residentialproperty.ResidentialPropertyId == default(int)) {
                // New entity
                var resProp = context.ResidentialProperty.Create();
                resProp.Address = context.Address.Create();

                resProp.Address.Line1 = residentialproperty.Address.Line1;
                resProp.Address.Line2 = residentialproperty.Address.Line2;
                resProp.Address.TownCity = residentialproperty.Address.TownCity;
                resProp.Address.County = residentialproperty.Address.County;
                resProp.Address.PostCode = residentialproperty.Address.PostCode;
                resProp.PropertyTypeString = residentialproperty.PropertyTypeString;
                resProp.SizeSquareMeters = residentialproperty.SizeSquareMeters;
                resProp.NumberOfBedrooms = residentialproperty.NumberOfBedrooms;
                resProp.NumberOfBathrooms = residentialproperty.NumberOfBathrooms;
                resProp.NumberOfReceptionRooms = residentialproperty.NumberOfReceptionRooms;
                resProp.Description = residentialproperty.Description;
                resProp.HasFrontGarden = residentialproperty.HasFrontGarden;
                resProp.HasSecureParking = residentialproperty.HasSecureParking;
                resProp.IsDisabledFriendly = residentialproperty.IsDisabledFriendly;
                resProp.DateAdded = residentialproperty.DateAdded;
                resProp.MonthlyRate = residentialproperty.MonthlyRate;

                var loggedIn = context.UserProfile.OfType<Landlord>().First(l => l.UserName == System.Web.HttpContext.Current.User.Identity.Name);
                resProp.UserId = loggedIn.UserId;
                resProp.AddressId = residentialproperty.AddressId;

                context.ResidentialProperty.Add(resProp);
                context.Address.Add(resProp.Address);
                Save();
            } else {
                // Existing entity
                context.Entry(residentialproperty).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var residentialproperty = context.ResidentialProperty.Find(id);
            context.ResidentialProperty.Remove(residentialproperty);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IResidentialPropertyRepository : IDisposable
    {
        IQueryable<ResidentialProperty> All { get; }
        IQueryable<ResidentialProperty> AllIncluding(params Expression<Func<ResidentialProperty, object>>[] includeProperties);
        ResidentialProperty Find(int id);
        void InsertOrUpdate(ResidentialProperty residentialproperty);
        void Delete(int id);
        void Save();
    }
}